import request from '@/utils/request'

// ==================== 新闻管理 ====================

// 分页查询新闻
export function getNewsPage(params) {
  return request({
    url: '/v1/news',
    method: 'get',
    params
  })
}

// 获取新闻详情
export function getNewsDetail(id) {
  return request({
    url: `/v1/news/${id}`,
    method: 'get'
  })
}

// 创建新闻
export function createNews(data) {
  return request({
    url: '/v1/news',
    method: 'post',
    data
  })
}

// 更新新闻
export function updateNews(id, data) {
  return request({
    url: `/v1/news/${id}`,
    method: 'put',
    data
  })
}

// 删除新闻
export function deleteNews(id) {
  return request({
    url: `/v1/news/${id}`,
    method: 'delete'
  })
}

// 发布新闻
export function publishNews(id) {
  return request({
    url: `/v1/news/${id}/publish`,
    method: 'post'
  })
}

// 下架新闻
export function unpublishNews(id) {
  return request({
    url: `/v1/news/${id}/unpublish`,
    method: 'post'
  })
}

// 置顶新闻
export function topNews(id) {
  return request({
    url: `/v1/news/${id}/top`,
    method: 'post'
  })
}

// 取消置顶
export function untopNews(id) {
  return request({
    url: `/v1/news/${id}/untop`,
    method: 'post'
  })
}

// 审核新闻
export function reviewNews(id, approved, comment) {
  return request({
    url: `/v1/news/${id}/review`,
    method: 'post',
    params: { approved, comment }
  })
}

// 获取热门新闻
export function getHotNews(limit = 10) {
  return request({
    url: '/v1/news/hot',
    method: 'get',
    params: { limit }
  })
}

// 获取推荐新闻
export function getRecommendNews(limit = 10) {
  return request({
    url: '/v1/news/recommend',
    method: 'get',
    params: { limit }
  })
}

// 获取置顶新闻
export function getTopNews(categoryId, limit = 5) {
  return request({
    url: '/v1/news/top',
    method: 'get',
    params: { categoryId, limit }
  })
}

// 搜索新闻
export function searchNews(keyword, limit = 20) {
  return request({
    url: '/v1/news/search',
    method: 'get',
    params: { keyword, limit }
  })
}

// 点赞新闻
export function likeNews(id) {
  return request({
    url: `/v1/news/${id}/like`,
    method: 'post'
  })
}

// 收藏新闻
export function favoriteNews(id) {
  return request({
    url: `/v1/news/${id}/favorite`,
    method: 'post'
  })
}

// 获取新闻统计
export function getNewsStatistics(id) {
  return request({
    url: `/v1/news/${id}/statistics`,
    method: 'get'
  })
}

// 更新新闻状态
export function updateNewsStatus(id, status) {
  return request({
    url: `/v1/news/${id}/status`,
    method: 'put',
    data: { status }
  })
}

// ==================== 新闻分类管理 ====================

// 获取分类列表
export function getNewsCategoryList() {
  return request({
    url: '/v1/news-categories',
    method: 'get'
  })
}

// 获取分类树
export function getNewsCategoryTree() {
  return request({
    url: '/v1/news-categories/tree',
    method: 'get'
  })
}

// 获取分类详情
export function getNewsCategoryDetail(id) {
  return request({
    url: `/v1/news-categories/${id}`,
    method: 'get'
  })
}

// 创建分类
export function createNewsCategory(data) {
  return request({
    url: '/v1/news-categories',
    method: 'post',
    data
  })
}

// 更新分类
export function updateNewsCategory(id, data) {
  return request({
    url: `/v1/news-categories/${id}`,
    method: 'put',
    data
  })
}

// 删除分类
export function deleteNewsCategory(id) {
  return request({
    url: `/v1/news-categories/${id}`,
    method: 'delete'
  })
}

// ==================== 新闻标签管理 ====================

// 获取标签列表
export function getNewsTagList() {
  return request({
    url: '/v1/news-tags',
    method: 'get'
  })
}

// 创建标签
export function createNewsTag(data) {
  return request({
    url: '/v1/news-tags',
    method: 'post',
    data
  })
}

// 更新标签
export function updateNewsTag(id, data) {
  return request({
    url: `/v1/news-tags/${id}`,
    method: 'put',
    data
  })
}

// 删除标签
export function deleteNewsTag(id) {
  return request({
    url: `/v1/news-tags/${id}`,
    method: 'delete'
  })
}

// ==================== 新闻评论管理 ====================

// 获取评论列表
export function getNewsCommentPage(params) {
  return request({
    url: '/v1/news-comments',
    method: 'get',
    params
  })
}

// 审核评论
export function reviewNewsComment(id, approved) {
  return request({
    url: `/v1/news-comments/${id}/review`,
    method: 'post',
    params: { approved }
  })
}

// 删除评论
export function deleteNewsComment(id) {
  return request({
    url: `/v1/news-comments/${id}`,
    method: 'delete'
  })
}

// ==================== 新闻详情页面相关 ====================

// 获取新闻统计（别名）
export function getNewsStats(newsId) {
  return getNewsStatistics(newsId)
}

// 获取新闻评论（别名）
export function getNewsComments(newsId, params) {
  return getNewsCommentPage({ ...params, newsId })
}

// 审核通过评论
export function approveComment(commentId) {
  return reviewNewsComment(commentId, true)
}

// 拒绝评论
export function rejectComment(commentId) {
  return reviewNewsComment(commentId, false)
}

// 删除评论（别名）
export function deleteComment(commentId) {
  return deleteNewsComment(commentId)
}

// 获取相关新闻
export function getRelatedNews(newsId) {
  return request({
    url: `/v1/news/${newsId}/related`,
    method: 'get'
  })
}

// 获取操作日志
export function getOperationLogs(newsId) {
  return request({
    url: `/v1/news/${newsId}/operation-logs`,
    method: 'get'
  })
}

// 推荐新闻
export function recommendNews(newsId) {
  return request({
    url: `/v1/news/${newsId}/recommend`,
    method: 'post'
  })
}

// 取消推荐
export function unrecommendNews(newsId) {
  return request({
    url: `/v1/news/${newsId}/unrecommend`,
    method: 'post'
  })
}

// 复制新闻
export function duplicateNews(newsId) {
  return request({
    url: `/v1/news/${newsId}/duplicate`,
    method: 'post'
  })
}
